import entity.Member;
import entity.MemberAddress;
import service.MemberAddressImpl;
import service.MemberImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Mybatis201759225309
 * @description: main
 * @author: Xiaozhuoer
 * @create: 2021-04-12 11:44
 **/

public class Main {
    public static void main(String[] args) {
        //addThreeMember();
        //addThreeMemberAddress();
        //delMemberFromName("Xiaozhuoer1");
        //updateMemberAddressCity(9,"杭州");
        //selectMemberAll();
        //selectMemberAddressAll();
        //selectMemberWhereIf(9,"Xiaozhuoer2","2222");
        //selectMemberTrimChoose(10,"Xiaozhuoer2","2222");
        //updateMemberAddressSet(9,null,"杭州",null,"5555");
        //selectMemberAddressForeach();
        //selectMemberAndMemberAddressByMemberIdOneToOne(8);
        //selectMemberAndMemberAddressByMemberIdOneToMore(9);
    }

    //Member表增加至少三条会员数据
    public static void addThreeMember(){
        MemberImpl memberImpl = new MemberImpl();

        Member member1 = new Member();
        member1.setUser_name("Xiaozhuoer1");
        member1.setPassword("xzer");
        member1.setEmail("1111@");
        member1.setSex(true);
        member1.setMobile("1111");
        memberImpl.addMember(member1);

        Member member2 = new Member();
        member2.setUser_name("Xiaozhuoer2");
        member2.setPassword("xzer");
        member2.setEmail("2222@");
        member2.setSex(true);
        member2.setMobile("2222");
        memberImpl.addMember(member2);

        Member member3 = new Member();
        member3.setUser_name("Xiaozhuoer3");
        member3.setPassword("xzer");
        member3.setEmail("3333@");
        member3.setSex(false);
        member3.setMobile("3333");
        memberImpl.addMember(member3);
    }

    //MemberAddress表增加至少三条会员地址数据，其中两条地址为同一个会员所有，且该会员应存在于Member表中
    public static void addThreeMemberAddress() {
        MemberAddressImpl memberAddressImpl = new MemberAddressImpl();

        MemberAddress memberAddress1 = new MemberAddress();
        memberAddress1.setMember_id(8);
        memberAddress1.setProvince("上海");
        memberAddress1.setCity("上海");
        memberAddress1.setAddress("嘉兴学院医学院");
        memberAddress1.setReceiver("1111");
        memberAddressImpl.addMemberAddress(memberAddress1);

        MemberAddress memberAddress2 = new MemberAddress();
        memberAddress2.setMember_id(8);
        memberAddress2.setProvince("浙江");
        memberAddress2.setCity("嘉兴");
        memberAddress2.setAddress("嘉兴学院梁林校区");
        memberAddress2.setReceiver("55555");
        memberAddressImpl.addMemberAddress(memberAddress2);

        MemberAddress memberAddress3 = new MemberAddress();
        memberAddress3.setMember_id(9);
        memberAddress3.setProvince("江苏");
        memberAddress3.setCity("苏州");
        memberAddress3.setAddress("嘉兴学院越秀校区");
        memberAddress3.setReceiver("66666");
        memberAddressImpl.addMemberAddress(memberAddress3);
    }

    //Member表按会员名称删除一条会员数据
    public static void delMemberFromName(String username){
        MemberImpl memberImpl = new MemberImpl();
        memberImpl.delMemberFromName(username);
    }

    //将MemberAddress表的某个会员地址中的city字段改为嘉兴
    public static void updateMemberAddressCity(int member_id,String newCity){
        MemberAddressImpl memberAddressImpl = new MemberAddressImpl();
        memberAddressImpl.updateMemberAddressCity(member_id,newCity);
    }

    //查找输出所有Member和MemberAddress表信息
    public static void selectMemberAll(){
        MemberImpl memberImpl = new MemberImpl();
        List<Member> memberList = memberImpl.selectMemberAll();
        for(Member member : memberList){
            System.out.println(member.toString());
        }
    }
    public static void selectMemberAddressAll(){
        MemberAddressImpl memberAddressImpl = new MemberAddressImpl();
        List<MemberAddress> memberAddressList = memberAddressImpl.selectMemberAddressAll();
        for(MemberAddress memberAddress : memberAddressList){
            System.out.println(memberAddress.toString());
        }
    }

    //分别使用Mybatis动态sql的where+if元素和trim+choose元素实现下面情境
    //根据“会员编号”、“会员姓名”、“手机号”查询会员信息
    public static void selectMemberWhereIf(int id, String username, String mobile){
        MemberImpl memberImpl = new MemberImpl();
        List<Member> memberList = memberImpl.selectMemberWhereIf(id,username,mobile);
        for(Member member : memberList){
            System.out.println(member.toString());
        }
    }
    public static void selectMemberTrimChoose(int id, String username, String mobile){
        MemberImpl memberImpl = new MemberImpl();
        List<Member> memberList = memberImpl.selectMemberTrimChoose(id,username,mobile);
        for(Member member : memberList){
            System.out.println(member.toString());
        }
    }

    //使用Mybatis动态sql的set元素实现下面情境
    //会员地址管理有更新“省份”、“城市”、“地址”、“手机号”、“接受人”的需求
    public static void updateMemberAddressSet(int member_id, String province, String city, String address, String receiver){
        MemberAddressImpl memberAddressImpl = new MemberAddressImpl();
        List<MemberAddress> memberAddressList = memberAddressImpl.updateMemberAddressSet(member_id,province,city,address,receiver);
        for(MemberAddress memberAddress : memberAddressList){
            System.out.println(memberAddress.toString());
        }
    }

    //使用Mybatis动态sql的foreach元素实现下面情境
    //会员地址根据地域划分，如江浙沪、鄂皖苏等
    public static void selectMemberAddressForeach(){
        MemberAddressImpl memberAddressImpl = new MemberAddressImpl();
        List<String> provinces= new ArrayList<>();
        provinces.add("上海");
        provinces.add("江苏");
        List<MemberAddress> memberAddressList = memberAddressImpl.selectMemberAddressForeach(provinces);
        for(MemberAddress memberAddress : memberAddressList){
            System.out.println(memberAddress.toString());
        }
    }

    //使用Mybatis的关联查询对member和member_address分别进行一对一和一对多查询
    //一对一
    public static void selectMemberAndMemberAddressByMemberIdOneToOne(int id){
        MemberImpl memberImpl = new MemberImpl();
        List<Member> memberList = memberImpl.selectMemberAndMemberAddressByMemberIdOneToOne(id);
        for(Member member : memberList){
            System.out.println(member.toString());
            for(MemberAddress memberAddress : member.getMemberAddresses()){
                System.out.println(memberAddress.toString());
            };
        }
    }
    //一对多
    public static void selectMemberAndMemberAddressByMemberIdOneToMore(int id){
        MemberImpl memberImpl = new MemberImpl();
        List<Member> memberList = memberImpl.selectMemberAndMemberAddressByMemberIdOneToMore(id);
        for(Member member : memberList){
            System.out.println(member.toString());
            for(MemberAddress memberAddress : member.getMemberAddresses()){
                System.out.println(memberAddress.toString());
            };
        }
    }
}
